package com.example.wjh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wjh.entity.LeaveApplicant;
import com.example.wjh.entity.Result;
import com.example.wjh.mapper.LeaveApplicantMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/leave")
public class LeaveApplicantController {

    @Autowired
    private LeaveApplicantMapper leaveApplicantMapper;

    // 计算球员未通过的请假申请数量
    @GetMapping("/player/pending/count")
    public Result getPlayerPendingLeaveCount() {
        QueryWrapper<LeaveApplicant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", "未通过")
                .eq("applicantType", "球员");
        long count = leaveApplicantMapper.selectCount(queryWrapper);
        return Result.success(count);
    }

    // 计算教练待审批的请假申请数量
    @GetMapping("/coach/pending/count")
    public Result getCoachPendingLeaveCount() {
        QueryWrapper<LeaveApplicant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", "未通过")
                .eq("applicantType", "教练");
        long count = leaveApplicantMapper.selectCount(queryWrapper);
        return Result.success(count);
    }

    // 审批请假申请
    @PutMapping("/approve/{id}")
    public String approveLeaveApplicant(@PathVariable Long id) {
        // 使用 UpdateWrapper 更新状态为通过
        int rows = leaveApplicantMapper.update(null, new UpdateWrapper<LeaveApplicant>()
                .eq("LeaveID", id)
                .set("Status", "通过"));
        return rows > 0 ? "审批成功" : "审批失败";
    }

    // 获取指定 ID 的请假申请信息
    @GetMapping("/{id}")
    public LeaveApplicant getLeaveApplicantById(@PathVariable Long id) {
        return leaveApplicantMapper.selectById(id);
    }

    // 添加新请假申请
    @PostMapping("/add")
    public String addLeaveApplicant(@RequestBody LeaveApplicant leaveApplicant) {
        int rows = leaveApplicantMapper.insertLeaveApplicant(leaveApplicant);
        return rows > 0 ? "请假申请添加成功" : "请假申请添加失败";
    }

    // 更新请假申请信息
    @PutMapping("/update/{id}")
    public String updateLeaveApplicant(@PathVariable Long id, @RequestBody LeaveApplicant leaveApplicant) {
        leaveApplicant.setLeaveId(id);  // 使用 setLeaveId 方法
        int rows = leaveApplicantMapper.updateLeaveApplicant(leaveApplicant);
        return rows > 0 ? "请假申请更新成功" : "请假申请更新失败";
    }

    // 删除指定 ID 的请假申请
    @DeleteMapping("/delete/{id}")
    public String deleteLeaveApplicant(@PathVariable Long id) {
        int rows = leaveApplicantMapper.deleteById(id);
        return rows > 0 ? "请假申请删除成功" : "请假申请删除失败";
    }

    // 批量删除
    @DeleteMapping("/delete/batch")
    public int deleteBatch(@RequestBody List<Long> ids) {
        return leaveApplicantMapper.deleteBatchIds(ids);
    }

    // 获取所有请假申请信息
    @GetMapping("/all")
    public List<LeaveApplicant> getAllLeaveApplicants() {
        return leaveApplicantMapper.getAllLeaveApplicants();
    }

    // 分页获取请假申请信息
    @GetMapping("/page")
    public IPage<LeaveApplicant> getLeaveApplicantList(@RequestParam("pageNum") Integer pageNum,
                                                       @RequestParam("pageSize") Integer pageSize) {
        Page<LeaveApplicant> page = new Page<>(pageNum, pageSize);
        QueryWrapper<LeaveApplicant> queryWrapper = new QueryWrapper<>();
        return leaveApplicantMapper.selectPage(page, queryWrapper);
    }

    // 多条件搜索请假申请
    @GetMapping("/search")
    public List<LeaveApplicant> searchLeaveApplicants(
            @RequestParam(required = false) String applicantName,
            @RequestParam(required = false) String applicantType,
            @RequestParam(required = false) String leaveType,
            @RequestParam(required = false) String status) {
        return leaveApplicantMapper.searchLeaveApplicants(applicantName, applicantType, leaveType, status);
    }
}
